package com.example.tests;

import org.junit.*;

import static org.junit.Assert.*;
import com.example.tests.logic.SortedListOf;
import com.example.tests.logic.User;
import com.example.tests.pages.LoginPage;
import factories.MyPageFactory;
import com.example.tests.pages.UserManagementPage;

public class UserTests extends Browser {

	private User user1;

	@Before
	public void setUpTest() {
		user1 = new User("user1", "user1@mail.ru", "qwerty12", "Editor");
	}

	@Test
	public void testAddUser() throws Exception {
		UserManagementPage UMPage = MyPageFactory.getPage(driver, LoginPage.class)
				.login(admin)
				.goUserManagementPage();
		assertTrue(UMPage.createUser(user1).isAbleToLogin(user1));
		MyPageFactory.getPage(driver, LoginPage.class).login(admin)
				.goUserManagementPage().deleteUser(user1);
	}

	@Test
	public void testAddUserAppearsInList() {
		UserManagementPage UMPage;
		UMPage = MyPageFactory.getPage(driver, LoginPage.class).login(admin)
				.goUserManagementPage();
		SortedListOf<User> oldUserList = UMPage.getUsers();
		User user = user1;
				
		if (oldUserList.contains(user)) {
			UMPage.deleteUser(user);
			oldUserList = UMPage.getUsers();
		}
		SortedListOf<User> newUserList = UMPage.createUser(user).getUsers();

		assertEquals(newUserList, oldUserList.withAdded(user));
	}
	@Test
	public void testRemovedUserDisappearsFromList() {
		UserManagementPage UMPage;
		SortedListOf<User> newUserList = new SortedListOf<User>();
		SortedListOf<User> oldUserList = new SortedListOf<User>();
		User user = user1;
		oldUserList = MyPageFactory.getPage(driver, LoginPage.class).login(admin)
						.goUserManagementPage()
						.createUser(user)
						.getUsers();
		UMPage = MyPageFactory.getPage(driver, UserManagementPage.class);
		if (oldUserList.contains(user)) {
			newUserList = UMPage.deleteUser(user).getUsers();
		}
		UMPage.logout();
		assertEquals(newUserList.withAdded(user), oldUserList);
	}

	@Test
	public void testRemoveUser() throws Exception {
		UserManagementPage UMPage = MyPageFactory.getPage(driver, LoginPage.class)
				.login(admin)
				.goUserManagementPage();
		assertTrue(UMPage.createUser(user1).isAbleToLogin(user1));
		assertTrue(UMPage.deleteUser(user1).isUserRemoved(user1));
		assertTrue(UMPage.isUserRemoved(user1));
		
	}

}
